package cn.amaake.magicplugin.rpc.service;

import cn.amaake.magicplugin.rpc.config.DubboConfig;
import cn.amaake.magicplugin.rpc.model.RpcInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.ssssssss.magicapi.core.model.*;
import org.ssssssss.magicapi.core.service.MagicResourceService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class DobboService {

    private final Logger logger = LoggerFactory.getLogger(DobboService.class);
    private MagicResourceService magicResourceService;
    private ApplicationContext applicationContext;
    private DubboConfig dubboConfig;
    private Map<String, MagicApiGenericService> serviceConfigMap = new HashMap<>();
    private String applicationname;

    public DobboService(ApplicationContext applicationContext, DubboConfig dubboConfig, MagicResourceService magicResourceService){
        this.applicationContext = applicationContext;
        this.dubboConfig = dubboConfig;
        this.magicResourceService = magicResourceService;
        initService();
    }




    //@PostConstruct
    private void initService(){
        applicationname = applicationContext.getEnvironment().getProperty("spring.application.name");
        magicResourceService.refresh();
        TreeNode<Group> rpcTreeNode = magicResourceService.tree("rpc");
        List<TreeNode<Group>> children = rpcTreeNode.getChildren();

        for(TreeNode<Group> a : children){
            String nodeid = a.getNode().getId();
            //String nodename = a.getNode().getName();
            String nodepath = a.getNode().getPath();
            List<RpcInfo> rpcInfoList = magicResourceService.listFiles(nodeid);
            String serviceName = nodepath.replace("/","");
            MagicApiGenericService genericService = new MagicApiGenericService(serviceName,rpcInfoList);
            if(!rpcInfoList.isEmpty()){
                genericService.registerService(dubboConfig,applicationname);
            }
            serviceConfigMap.put(serviceName, genericService);
        }

    }

    public void registerService(String serviceName,RpcInfo rpcInfo,List<RpcInfo> rpcInfoList){
        MagicApiGenericService magicApiGenericService = serviceConfigMap.get(serviceName);
        //List<RpcInfo> rpcInfoList = magicResourceService.listFiles(rpcInfo.getGroupId());
        if(magicApiGenericService == null){
            MagicApiGenericService genericService = new MagicApiGenericService(serviceName,rpcInfoList);
            if(!rpcInfoList.isEmpty()){
                genericService.registerService(dubboConfig,applicationname);
            }
            serviceConfigMap.put(serviceName, genericService);
        }else{
            magicApiGenericService.setRpcInfoList(rpcInfoList);
            magicApiGenericService.countIsEnable();
            if (magicApiGenericService.getServiceServiceConfig() == null) {
                magicApiGenericService.registerService(dubboConfig,applicationname);
            }else{
                if(magicApiGenericService.isEnable()){
                    if(!magicApiGenericService.getServiceServiceConfig().isExported()){
                        magicApiGenericService.setServiceServiceConfig(null);
                        magicApiGenericService.registerService(dubboConfig,applicationname);
                    }
                }else{
                    if(magicApiGenericService.getServiceServiceConfig().isExported()){
                        magicApiGenericService.getServiceServiceConfig().unexport();
                        magicApiGenericService.setServiceServiceConfig(null);
                    }
                }
            }
        }

    }

    public void unexport(String serviceName,RpcInfo rpcInfo,List<RpcInfo> rpcInfoList){
        MagicApiGenericService magicApiGenericService = serviceConfigMap.get(serviceName);
        if(magicApiGenericService!=null){
            magicApiGenericService.setRpcInfoList(rpcInfoList);
            magicApiGenericService.countIsEnable();
            if (magicApiGenericService.getServiceServiceConfig() != null) {
                if(!magicApiGenericService.isEnable()){
                    if(magicApiGenericService.getServiceServiceConfig().isExported()){
                        magicApiGenericService.getServiceServiceConfig().unexport();
                        magicApiGenericService.setServiceServiceConfig(null);
                    }
                }
            }
            if (magicApiGenericService.getRpcInfoList().isEmpty()){
                serviceConfigMap.remove(serviceName);
            }
        }

    }

    public Map<String, MagicApiGenericService> getServiceConfigMap() {
        return serviceConfigMap;
    }

    public void setServiceConfigMap(Map<String, MagicApiGenericService> serviceConfigMap) {
        this.serviceConfigMap = serviceConfigMap;
    }
}
